'''
上传文件
files: (optional) Dictionary of ``'name': file-like-objects``
         (or ``{'name': file-tuple}``) for multipart encoding upload.
        ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``,
        3-tuple ``('filename', fileobj, 'content_type')``
        or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``
'''

import requests

# 接口测试网站，用户发送请求，该网站把收到的请求，
# 组装成json格式的响应，返回给用户。
# /post 是post方法
# /get 是get方式
# /delete 是delete方法

url = "http://www.httpbin.org/post"
cs = {
    "user": "admin",
    "e-mail": "123213@qq.com"
}
path = "c:/test.txt"
with open(path,mode='r') as f:
    fs = {
        # "file" 上传接口的参数名，是接口定义的。
        # "file":(path,f) # 二元祖：（文件名字，文件对象）
        "file1":(path,f,"text/plain") # 三元组：(文件名字，文件对象，文件类型)
    }
    r = requests.post(url,data=cs,files= fs)
    print(r.text)

# 租车系统，添加车辆时，先上传车辆的图片
path = 'c:/test.jpg'
url = "http://192.168.2.36:8089/carRental/file/uploadFile.action"
with open(path,mode='rb')as f:
    fs = {
        "mf": (path,f,"image/jpg") # 三元组：(文件名字，文件对象，文件类型)
    }
    r = requests.post(url,data=cs,files=fs)
    print(r.text)
    assert "2021-03-18" in r.json()['data']['src']

# 一次上传多个文件（接口支持一次传多个文件）
print("*"*50) # 分割线
url = "http://www.httpbin.org/post"
path1 = "c:/test.txt"
path2 = 'c:/test.jpg'
with open(path1,mode='r')as f1:
    with open(path2,mode='rb')as f2:
        fs={
            "f1": (path,f1),
            "f2":(path,f2,"image/jpg")
        }
        r = requests.post(url,files = fs)
        print(r.text)